package org.web2024.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import org.web2024.dao.*;
import org.web2024.service.StudentService;
import org.web2024.dao.OptionalCourse;
import java.util.HashMap;
import java.util.List;


@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;


    @PostMapping("/showAllCourse")
    public Result selectAllCourse() {
        List<Course> courseList = studentService.showAllCourse();
        if (courseList.isEmpty()) {
            return Result.fail("暂无课程");
        }
        return Result.success(courseList);
    }

    @PostMapping("/showSelectedCourse")
    public Result showSelectCourse(@RequestBody OptionalCourse optionalcourse) {
        String selCyear= optionalcourse.getSelCyear();
        String sno=((UserdetailsImp) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUser().getUser_no();
        return Result.success(studentService.showSelectedCourse(sno, selCyear));
    }

    @PostMapping("/showStudentInfo")
    public Result showStudentInfo() {
        String sno=((UserdetailsImp) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUser().getUser_no();
        Student studentInfo = studentService.showStudentInfo(sno);
        return Result.success(studentInfo);
    }

    @PostMapping("/showOptionalCourse")
    public Result showOptionalCourse(@RequestBody OptionalCourse optionalcourse) {
        String typestr = "";
        Integer selCtype = optionalcourse.getSelCtype();
        String selCyear= optionalcourse.getSelCyear();
        String selSno = ((UserdetailsImp) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUser().getUser_no();
        System.out.println("selSno: " + selSno);
        System.out.println("selCtype: " + selCtype);
        System.out.println("selCyear: " + selCyear);
        if (selCtype != null) {
            if (selCtype == 1) {
                typestr = "00";
            } else if (selCtype == 3) {
                typestr = "01";
            } else if (selCtype == 2 && selSno != null) {
                typestr = String.valueOf(selSno).substring(3, 5);
            } else {
                return Result.fail("无效的选课类型");
            }
        } else {
            return Result.fail("选课类型不能为空");
        }

        List<Course> courseList = studentService.showOptionalCourse(typestr, selCyear);

        if (courseList.isEmpty()) {
            return Result.fail("无可选课程");
        }
        System.out.println(courseList);
        return Result.success(courseList);
    }
    @PostMapping("/modifyStudentInfo")
    public Result modifyStudentinfo(@RequestBody Student student) {
        String s_email=student.getS_email();
        System.out.println(s_email);
        String sno=((UserdetailsImp) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUser().getUser_no();
        return Result.success(studentService.modifyStudentinfo(sno, s_email));
    }
    @PostMapping("/searchCourse")
    public Result searchCourse(@RequestBody Course course) {
        String c_no=course.getC_no();
        HashMap<String, Object> searchCourse = studentService.searchCourse(c_no);
        if (searchCourse.get("course")==null) {
            return Result.fail("未查询到课程");
        }
        return Result.success(searchCourse);
    }
    @PostMapping("/confirmCourse")
    public Result confirmCourse(@RequestParam String c_no,@RequestParam int coins) {
        if(coins<0)
            return Result.fail("选课积分不能小于0");
        if(!studentService.confirmCourse(c_no, coins)){
            return Result.fail("当前已选课");
        }
        return Result.success();
    }

    @PostMapping("/deleteCourse")
    public Result deleteCourse(@RequestParam("cno") String c_no) {
        if(!studentService.deletecourse(c_no)){
            return Result.fail("未作选课");
        }
        return Result.success();
    }
}
